package com.hejia.bulbul.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.Sentence;
import com.hejia.alauda.service.AccountService;
import com.hejia.alauda.service.SentenceService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

/**
 * 
 * @author jatin
 *
 */
@Controller
@RequestMapping("sentence")
public class SentenceController extends BaseController {

	private static final Log logger = LogFactory
			.getLog(QuestionController.class);

	@Resource
	private SentenceService sentenceService;

	@Resource
	private AccountService accountService;

	@RequestMapping
	public String list(Model model, Pager<Sentence> pager, String content) {

		pager = this.sentenceService.findSentences(pager, content);
		model.addAttribute("content", content);
		model.addAttribute("sentences", pager);

		// 分页
		PagerHelper<Sentence> helper = new PagerHelper<>();
		helper.setPager(pager);
		Map<String, String> map = new HashMap<>();
		if (content != null) {
			map.put("ask", content);
		}
		helper.setParams(map);
		helper.setBaseUrl("/sentence");
		model.addAttribute("pagerHelper", helper);

		return "sentence/list";
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(Sentence sentence) {

		HttpResponse response = new HttpResponse();

		try {
			UserDetails currentAccount = this.accountService
					.getCurrentAccount();
			if (currentAccount != null) {
				sentence.setEditor(currentAccount.getUsername());
			}

			sentence = sentenceService.addSentence(sentence);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("添加每日一句成功");
			response.setData(sentence);

		} catch (Exception e) {
			logger.error("添加每日一句失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("添加每日一句失败 " + e.getMessage());
			response.setData(sentence);
		}

		return response;
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id) {

		HttpResponse response = new HttpResponse();

		try {
			sentenceService.deleteSentence(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除每日一句成功");

		} catch (Exception e) {
			logger.error("删除每日一句失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除每日一句失败 " + e.getMessage());
		}

		return response;
	}

	@RequestMapping("{id}/edit")
	public String edit(Model model, @PathVariable Long id) {

		Sentence sentence = this.sentenceService.findSentenceById(id);
		model.addAttribute("sentence", sentence);

		return "sentence/edit";
	}

	@RequestMapping(value = "{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse update(@PathVariable Long id, Sentence sentence) {

		HttpResponse response = new HttpResponse();

		try {
			UserDetails currentAccount = this.accountService
					.getCurrentAccount();
			if (currentAccount != null) {
				sentence.setEditor(currentAccount.getUsername());
			}
			this.sentenceService.updateSentence(sentence);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setData(sentence);

		} catch (Exception e) {
			logger.error("每日一句更新失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("每日一句更新失败 " + e.getMessage());
		}

		return response;
	}

}
